<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="患者名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入患者名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="性别" prop="gender">
              <el-select v-model="queryParams.gender" placeholder="请选择性别">
                <el-option v-for="item in sys_user_sex" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:patient:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="patientList">
        <el-table-column v-if="true" label="ID" align="center" prop="id" width="180" />
        <el-table-column label="昵称" align="center" prop="nickname" />
        <el-table-column label="患者名称" align="center" prop="name" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="性别" align="center" prop="gender">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.gender" />
          </template>
        </el-table-column>
        <el-table-column label="生日" align="center" prop="birthday" width="180" />
        <el-table-column label="手机号" align="center" prop="mobile" />
        <el-table-column label="身份证号" align="center" prop="idCard" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="备注" placement="top">
              <el-button v-hasPermi="['system:registration:remark']" link type="primary" icon="EditPen" @click="handleRemark(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 备注对话框 -->
    <el-dialog v-model="remarkDialog.visible" :title="remarkDialog.title" width="500px" append-to-body>
      <el-form ref="registrationFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="remarkubmitForm">确 定</el-button>
          <el-button @click="remarkDialog.visible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Patient" lang="ts">
import { listWxUser, updateWxUser } from '@/api/system/wxUser';
import { userVO, userQuery, userForm } from '@/api/system/wxUser/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_user_sex } = toRefs<any>(proxy?.useDict('sys_user_sex'));

const patientList = ref<userVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const userFormRef = ref<ElFormInstance>();

const remarkDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: userForm = {};
const data = reactive<PageData<userForm, userQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    gender: undefined,
    mobile: undefined,
    name: undefined
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询就诊卡列表 */
const getList = async () => {
  loading.value = true;
  const res = await listWxUser(queryParams.value);
  patientList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  userFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 备注按钮操作 */
const handleRemark = async (row?: userVO) => {
  reset();
  form.value.id = row.id;
  form.value.remark = row.remark;
  remarkDialog.visible = true;
  remarkDialog.title = '备注';
};

/** 备注提交按钮 */
const remarkubmitForm = async () => {
  await updateWxUser({
    id: form.value.id,
    remark: form.value.remark
  }).finally(() => (buttonLoading.value = false));
  proxy?.$modal.msgSuccess('操作成功');
  remarkDialog.visible = false;
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/member/export',
    {
      ...queryParams.value
    },
    `user_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
